﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Volo.Abp;
using Volo.Abp.Domain.Values;

namespace Mozhi.Abp.Domain.BasicData
{
    public class Warehouse : ValueObject
    {
        [JsonProperty]
        public Guid Id { get; private set; }

        [JsonProperty]
        public string Code {  get; private set; }

        [JsonProperty]
        public string Name { get; private set; }

        [JsonProperty]
        public string Address {  get; private set; }

        [JsonProperty]
        public bool ActivateLocation { get; private set; }

        [JsonProperty]
        public bool Activated {  get; private set; }

        [JsonProperty]
        public List<Location> Locations { get; private set; }

        private Warehouse() { }

        public void Validate()
        {
            if (Activated == false)
                throw new UserFriendlyException($"仓库：{Code}-{Name}已禁用", "100000");
        }

        public void ValidateLoaction(Guid? locationBinId)
        {
            if (!ActivateLocation && locationBinId.HasValue)
                throw new UserFriendlyException($"仓库：{Code}-{Name}未启用库位，不允许使用库位", "100000");

            if (ActivateLocation && !locationBinId.HasValue)
                throw new UserFriendlyException($"仓库：{Code}-{Name}已启用库位，必须填入库位", "100000");

            var match = Locations?.FirstOrDefault(x => x.Id == locationBinId);

            if (match == null)
                throw new UserFriendlyException($"仓库：{Code}-{Name}库位：{locationBinId}不存在", "100000");
        }

        protected override IEnumerable<object> GetAtomicValues()
        {
            throw new NotImplementedException();
        }
    }

    public class Location : ValueObject
    {
        [JsonProperty]
        public Guid Id { get; private set; }

        [JsonProperty]
        public string Name { get; private set; }

        protected override IEnumerable<object> GetAtomicValues()
        {
            throw new NotImplementedException();
        }
    }

}
